﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using LogisticsBLL;
using System.Text.RegularExpressions;

namespace WebLosgic.Accounting
{
    public partial class CostAccounting : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DDLbind();
            }
            
        }

        public void DDLbind()
        {
            TruckBLL tb = new TruckBLL();
            DropDownList1.DataSource = tb.ddlbindCarName();
            DropDownList1.DataTextField = "TeamName";
            DropDownList1.DataValueField = "TeamName";
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, new ListItem("-请选择-", "0"));
        }

        /// <summary>
        /// 引出报表
        /// </summary>
        /// <param name="m_DataTable"></param>
        /// <param name="s_FileName"></param>
        /// <returns></returns>
        public string DataToExcel(DataTable m_DataTable, string s_FileName)
        {
            string FileName = AppDomain.CurrentDomain.BaseDirectory + ("/Upload/Excel/") + s_FileName + ".xls";  //文件存放路径
            if (System.IO.File.Exists(FileName))                                //存在则删除
            {
                System.IO.File.Delete(FileName);
            }
            System.IO.FileStream objFileStream;
            System.IO.StreamWriter objStreamWriter;
            string strLine = "";
            objFileStream = new System.IO.FileStream(FileName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
            objStreamWriter = new System.IO.StreamWriter(objFileStream, Encoding.Unicode);
            for (int i = 0; i < m_DataTable.Columns.Count; i++)
            {
                strLine = strLine + m_DataTable.Columns[i].Caption.ToString() + Convert.ToChar(9);      //写列标题
            }
            objStreamWriter.WriteLine(strLine);
            strLine = "";
            for (int i = 0; i < m_DataTable.Rows.Count; i++)
            {
                for (int j = 0; j < m_DataTable.Columns.Count; j++)
                {
                    if (m_DataTable.Rows[i].ItemArray[j] == null)
                        strLine = strLine + " " + Convert.ToChar(9);                                    //写内容
                    else
                    {
                        string rowstr = "";
                        rowstr = m_DataTable.Rows[i].ItemArray[j].ToString();
                        if (rowstr.IndexOf("\r\n") > 0)
                            rowstr = rowstr.Replace("\r\n", " ");
                        if (rowstr.IndexOf("\t") > 0)
                            rowstr = rowstr.Replace("\t", " ");
                        strLine = strLine + rowstr + Convert.ToChar(9);
                    }
                }
                objStreamWriter.WriteLine(strLine);
                strLine = "";
            }
            objStreamWriter.Close();
            objFileStream.Close();
            return FileName;        //返回生成文件的绝对路径
        }

        protected void BtnCost_Click(object sender, EventArgs e)
        {
            if(DropDownList1.SelectedValue!="0" && TxtChenDate.Text!="" && Txtchengdata.Text != "")
            {
                CostBLL cot = new CostBLL();
                string TeamName = DropDownList1.SelectedValue;
                DateTime ChenDate = Convert.ToDateTime(TxtChenDate.Text);
                DateTime ReceDateEnd = Convert.ToDateTime(Txtchengdata.Text);
                DataTable tab = cot.tabmakeExcel(TeamName, ChenDate, ReceDateEnd);
                if (tab.Rows.Count > 0)
                {
                    string FileName = "核算报表2";
                    DataToExcel(tab, FileName);
                    Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('核算成功，已经生成报表！')", true);
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(GetType(),"","alert('没有该车队在这个时间段的信息，不能生成报表')", true);
                }
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('请选择车队和填写时间')", true);
            }
            
        }

        protected void BtnReset_Click(object sender, EventArgs e)
        {

            DropDownList1.SelectedValue = "0";
            TxtChenDate.Text = "";
            Txtchengdata.Text= "";
        }

        protected void BtnPOST_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(@"C:\Users\Administrator\Desktop\物流管理-项目答辩\Rapid LMS\WebLosgic\WebLosgic\Upload\Excel");
        }
    }
}